package test.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.config.gui.AbstractConfigGui;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import kg.apc.jmeter.JMeterPluginsUtils;


/**
 * 当是在gui上创建一个sampler，其调用的顺序是**clearGui()->createTestElement()->modifyTestElement()->configure()**
 * @author Administrator
 *
 */
public class SubSampleDefaultConfigGui extends AbstractConfigGui{
	
	public static final Logger log=LoggingManager.getLoggerForClass();
	private static final long serialVersionUID=241L;

	
	private JTextField writea;
	public JTextField getWritea() {
		return writea;
	}

	public JTextField getWritab() {
		return writab;
	}
	private JTextField writab;
	
	public SubSampleDefaultConfigGui() {
		// TODO Auto-generated constructor stub
		super();
		init();
	}
	

	//创建窗体
	private void init() {
		// TODO Auto-generated method stub
		setLayout(new BorderLayout(0,5));
		setBorder(makeBorder());
		add(createPanel(), BorderLayout.CENTER);
		
	}


	private Component createPanel() {
		final JPanel totalPanel=new VerticalPanel();
		totalPanel.setBorder(makeBorder());
		//先加标题栏
		totalPanel.add(makeTitlePanel());
		//a+b
		JPanel addab=new HorizontalPanel();
		addab.setBorder(BorderFactory.createTitledBorder("实现a+b"));//这是标题
		
		JLabel aLabel=new JLabel("输入a:");
		writea=new JTextField(15);
		aLabel.setLabelFor(aLabel);//注意这里
		addab.add(aLabel);
		addab.add(writea);
		
		JLabel jia=new JLabel("+");
		addab.add(jia);
		
		JLabel bLabel=new JLabel("b:");
		writab=new JTextField(15);
		addab.add(bLabel);
		addab.add(writab);
	
		totalPanel.add(addab);
		return totalPanel;
		
		
		
	}
	
	//设置sample名称
	@Override
	public String getStaticLabel() {
		// TODO Auto-generated method stub
		return JMeterPluginsUtils.prefixLabel("SubSampleDefaultConfig");
	}
	@Override
	public String getLabelResource() {
		// TODO Auto-generated method stub
		return getClass().getSimpleName();
	}
	

	//创建新的sample,并传入到modifyTestElement中
	//似乎是固定的格式
	@Override
	public TestElement createTestElement() {
		// TODO Auto-generated method stub
		ConfigTestElement element = new ConfigTestElement();
		modifyTestElement(element);
		return element;
	}

	@Override
	public void modifyTestElement(TestElement element) {
		element.clear();
		configureTestElement(element);//这个必写
		//super.configureTestElement(element);//这个必写
		//super.configure(element);
		//practiseDemo01Sample sample=(practiseDemo01Sample) element;
		element.setProperty(SubSample.text1, writea.getText());
		element.setProperty(SubSample.text2, writab.getText());
		//sample.setA(writea.getText());
		//sample.setB(writab.getText());
	}
	@Override
	public void clearGui() {
		// TODO Auto-generated method stub
		super.clearGui();
		writab.setText("");
		writea.setText("");
	}
	@Override
	public void configure(TestElement element) {
		// TODO Auto-generated method stub
		super.configure(element);
		writab.setText(element.getPropertyAsString(SubSample.text2));
		writea.setText(element.getPropertyAsString(SubSample.text1));
		
	}
	
}
